package main

import (
	"strings"
	"time"
)

// Duration time duration
type Duration time.Duration

// ToDuration convert to time.Duration
func (d Duration) ToDuration() time.Duration {
	return time.Duration(d)
}

// MarshalJSON implement json interface
func (d *Duration) MarshalJSON() ([]byte, error) {
	s := d.ToDuration().String()
	return []byte(s), nil
}

// UnmarshalJSON implment json interface
func (d *Duration) UnmarshalJSON(data []byte) error {
	var dt time.Duration
	var err error
	s := strings.Trim(string(data), `"`)
	if dt, err = time.ParseDuration(s); err != nil {
		return err
	}
	*d = Duration(dt)
	return nil
}
